

function addCourse(){

 	var data = "";
	
	//get the course id form form
	var courseIdNode = document.getElementById('courseid');
	var courseId = courseIdNode.options[courseIdNode.selectedIndex].value;
	data += "id=" + courseId;
	
	//get the date from form
	var courseDateD = document.getElementById('coursedate_day').value;
	data += "&dateday=" + encodeURIComponent(courseDateD);
	
	var courseDateM = document.getElementById('coursedate_month').value;
	data += "&datemonth=" + encodeURIComponent(courseDateM);
	
	var courseDateY = document.getElementById('coursedate_year').value;
	data += "&dateyear=" + encodeURIComponent(courseDateY);
	
	var courseDate = courseDateM.trim() + "/" + courseDateD.trim() + "/" + courseDateY.trim();
	if (!isDate(courseDate)){
		alert("Please enter a valid course date");
		return;
	}
	XMLRequestPostQ("courses/services.php?method=addcourse", data, addCourseCallback);
	
}

function addCourseCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	//clear date box
	document.getElementById('coursedate_day').value = "dd";
	document.getElementById('coursedate_month').value = "mm";
	document.getElementById('coursedate_year').value = "yyyy";
	
	//get title & date & id from xmlDoc and add to 
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var courseId = idNode[0].firstChild.nodeValue;

	var dateNode = xmlDoc.documentElement.getElementsByTagName('course_date');
	var courseDate = dateNode[0].firstChild.nodeValue;

	var courseNode = xmlDoc.documentElement.getElementsByTagName('coursename');
	var courseName = courseNode[0].firstChild.nodeValue;

	var cRow = document.createElement("tr");
	cRow.setAttribute("id","courserow"+courseId);
	changeNodeClass(cRow,"vp");
	
	var dateF = document.createElement("td");
	dateF.appendChild(document.createTextNode(courseDate));
	changeNodeClass(dateF,"vp");
	cRow.appendChild(dateF);
	
	var nameF = document.createElement("td");
	nameF.appendChild(document.createTextNode(courseName));
	changeNodeClass(nameF,"vp");
	cRow.appendChild(nameF);

	var optionF = document.createElement("td");
	changeNodeClass(optionF,"vp");
	var removeSpan = document.createElement("span");
	
	if(removeSpan.addEventListener){ // Mozilla, Netscape, Firefox
		removeSpan.addEventListener('click', function(){removeCourse(courseId,courseName);}, false);
	} else { // IE
		removeSpan.attachEvent('onclick', function(){removeCourse(courseId,courseName);});
	}
	changeNodeClass(removeSpan,'remove');

	removeSpan.appendChild(document.createTextNode("[remove]"));
	optionF.appendChild(removeSpan);
	cRow.appendChild(optionF);

	var cTable = document.getElementById("userscoursesheader");

	cTable.parentNode.appendChild(cRow);

}


function removeCourse(courseUserId,courseTitle){
	if (confirm("Are you sure you want to remove the course '"+courseTitle+"'?")){
		XMLRequestQ("courses/services.php?method=removecourse&cuid=" + courseUserId, removeCourseCallback);
	}
}

function removeCourseCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	// extract the Id
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var courseId = idNode[0].firstChild.nodeValue;
	
	//remove the node
	var row = document.getElementById('courserow'+courseId);
	row.parentNode.removeChild(row);
}

